package com.canbot.u05.activity;

import android.app.Dialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.Gravity;
import android.view.Window;
import android.view.WindowManager;

import com.canbot.u05.MainApplication;
import com.canbot.u05.R;
import com.canbot.u05.socket.MsgSendUtils;
import com.canbot.u05.socket.MsgType;
import com.canbot.u05.utils.Config;
import com.canbot.u05.utils.Logger;
import com.canbot.u05.utils.ToastTools;
import com.canbot.u05.utils.WifiUtils;
import com.canbot.u05.view.CenterDialog;
import com.canbot.u05.view.CenterDialog2;
import com.canbot.u05.view.IButtonClickListerner;

/**
 * Created by ${lixiaoxiong} on 2017/7/3.
 */

public class ConnectionLostActivity extends BaseActivityNoTitle {

        private static final int CONN_HINT = 6;

        private static final int mobileSuccess = 1;

        private CenterDialog2 centerDialog;

        private CenterDialog shutdawnDialog;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.act_confirm_shutdown);
                this.registerReceiver(mSendResultReceiver, new IntentFilter("com.canbot.u05.isConnect"));
                //确定
//                MsgSendUtils.sendStringMsg(MsgType.SEND_SHUT_DOWN_CONFIRM,"shutdown");
//                Intent intent = new Intent(ConfirmShutdownDialogActivity.this,ShutdownActivity.class);
//                startActivity(intent);
//开始监听网络组播信息.
//finish();
//取消
//                MsgSendUtils.sendStringMsg(MsgType.SEND_SHUT_DOWN_CANCLE,"shutdownCancle");
                shutdawnDialog = new CenterDialog(this, R.style.centerdialog, new IButtonClickListerner() {
                        @Override
                        public void doBtnRight() {  //确定
//                MsgSendUtils.sendStringMsg(MsgType.SEND_SHUT_DOWN_CONFIRM,"shutdown");
//                Intent intent = new Intent(ConfirmShutdownDialogActivity.this,ShutdownActivity.class);
//                startActivity(intent);
                                shutdawnDialog.dismiss();
                                centerDialog = new CenterDialog2(ConnectionLostActivity.this, CONN_HINT);
                                centerDialog.show();
                                setDialogWindowAttr(centerDialog, ConnectionLostActivity.this, (int) getResources().getDimension(R.dimen.x500), (int) getResources().getDimension(R.dimen.x200));
                                //开始监听网络组播信息.
                                if (!MainApplication.isConnect) {
                                        MainApplication.getInstance().getServerIp();
                                }
                                String currentSSID = getCurrentSSID();
                                if (WifiUtils.isU05Wifi(currentSSID)) {
                                        MsgSendUtils.sendStringMsgToPad(MsgType.SEND_PAD_CONN_HOTSPOT, currentSSID);
                                }
                                else {
                                        ToastTools.newInstance(ConnectionLostActivity.this).showText(getString(R.string.not_connected_chest_hotspot));
                                }
                                //finish();
                        }

                        @Override
                        public void doBtnLeft() {   //取消
//                MsgSendUtils.sendStringMsg(MsgType.SEND_SHUT_DOWN_CANCLE,"shutdownCancle");
                                shutdawnDialog.dismiss();
                                finish();
                        }
                }, 7);

                shutdawnDialog.show();

        }

        private String getCurrentSSID() {
                //获取当前连接的wifi信息
                WifiManager wifiManager = (WifiManager) getApplicationContext().getSystemService(WIFI_SERVICE);
                WifiInfo wifiInfo = wifiManager.getConnectionInfo();
                if (wifiInfo != null) {
                        return wifiInfo.getSSID();
                }
                return null;


        }

        @Override
        protected void onResume() {
                super.onResume();

        }

        @Override
        protected void onDestroy() {
                super.onDestroy();
                unregisterReceiver(mSendResultReceiver);
        }

        BroadcastReceiver mSendResultReceiver = new BroadcastReceiver() {
                public void onReceive(Context context, Intent intent) {
                        if (centerDialog != null) {
                                centerDialog.cancel();
                        }
                        Logger.e("弹框消失", "-------");
                        centerDialog = new CenterDialog2(ConnectionLostActivity.this, mobileSuccess);
                        centerDialog.show();
                        setDialogWindowAttr(centerDialog, ConnectionLostActivity.this, (int) getResources().getDimension(R.dimen.x500), (int) getResources().getDimension(R.dimen.x200));
                        handler.sendEmptyMessageDelayed(mobileSuccess, 1 * 1000);
                }
        };

        //设置对话框显示
        public static void setDialogWindowAttr(Dialog dlg, Context ctx, int width, int height) {
                Window window = dlg.getWindow();
                WindowManager.LayoutParams lp = window.getAttributes();
                lp.gravity = Gravity.CENTER;
                lp.width = width;//宽高可设置具体大小
                lp.height = height;
                dlg.getWindow().setAttributes(lp);
        }

        Handler handler = new Handler() {
                @Override
                public void handleMessage(Message msg) {
                        switch (msg.what) {
                                case mobileSuccess:
                                        if (centerDialog != null) {
                                                centerDialog.cancel();
                                        }

                                        ConnectionLostActivity.this.finish();
                                        Log.d("ConnectionLostActivity", "成功组网");
                                        break;
                                case CONN_HINT:
                                        if (centerDialog != null) {
                                                centerDialog.cancel();
                                        }
                                        break;
                        }

                }
        };
}
